package com.bestillet.defaultmultishop;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class CheckDialog extends Dialog {
	private MixActivity context;
	private ListView list;
	private Button add;
	
	private Mix mix;
	
	private OnItemClickListener item = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			CheckBox check = (CheckBox) view.findViewById(R.id.chkOption);
			boolean checked = mix.getChoices().contains(mix.getOptions().get(position));
			check.setChecked(!checked);
			
			if (!checked)
				mix.getChoices().add(mix.getOptions().get(position));
			else
				mix.getChoices().remove(mix.getOptions().get(position));
		}
	};
	
	private View.OnClickListener click = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			context.updateChoices();
			dismiss();
		}
	};
	
	public CheckDialog(Context context, int index, Mix mix) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_check);
		
		this.context = (MixActivity) context;
		this.mix = mix;
		
		CheckArrayAdapter adapter = new CheckArrayAdapter(context, mix.getOptions(), mix.getChoices());
		list = (ListView) findViewById(android.R.id.list);
		list.setAdapter(adapter);
		list.setOnItemClickListener(item);
		
		add = (Button) findViewById(R.id.btnDone);
		add.setOnClickListener(click);
	}
}